package com.desheng.Controller;


import com.desheng.Service.BookService;
import com.desheng.bean.Item;
import com.desheng.bean.User;
import com.easyFramework.annotation.Autowired;
import com.easyFramework.annotation.Controller;
import com.easyFramework.annotation.RequestMapping;
import com.easyFramework.annotation.RequestParam;
import com.easyFramework.bean.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yezl
 * @date 2018/6/1 0:51
 * @decription this is :
 */
//@Controller("book")
    @Controller
    @RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;

    @RequestMapping("/getbook.do")
    public ModelAndView getbook(Item item) {

        System.out.println("你点击的是" + item.getItemid());
        ModelAndView modelAndView = new ModelAndView();
        Item it = bookService.getBook(item.getItemid());

        modelAndView.addModel("book", it);
        modelAndView.getModel().put("keyword", it.getBookname());
        modelAndView.setPath("introduction.jsp");
        return modelAndView;

    }

    @RequestMapping("/introduction")
    public ModelAndView introduction(@RequestParam("itemid") Integer itemid) {

        System.out.println("你点击的是" + itemid);
        ModelAndView modelAndView = new ModelAndView();
        Item it = bookService.getBook(itemid);

        modelAndView.addModel("book", it);
        modelAndView.getModel().put("keyword", it.getBookname());
        modelAndView.setPath("introduction.jsp");
        return modelAndView;

    }


    @RequestMapping("/search.do")
    public ModelAndView search(@RequestParam("keyword") String keyword,
                               @RequestParam("sort") Integer sort,
                               @RequestParam("start") Integer start,
                               @RequestParam("limit") Integer limit,
                               HttpServletRequest request) {
        HttpSession httpSession = request.getSession();
        int nextsort = 1;
        System.out.println("sort" + sort);
        System.out.println("start" + start);
        System.out.println("limit" + limit);
        String sortStr = "itemid desc";
        if (sort != null) {
            if (sort == 1) {   //价格从小到大，
                sortStr = "price asc";
            } else if (sort == -1) {//价格从大到小，
                sortStr = "price desc";
            }
            if(sort!=0)
                nextsort = -sort;
        }

        if (start == null) {
            start = 1;
        }

        if (limit == null) {
            limit = 8;
        }
        if (start <= 0) {
            start = 1;
        }

        System.out.println("判断后面");
        System.out.println("sort" + sort);
        System.out.println("start" + start);
        System.out.println("limit" + limit);
        int starts = limit * (start - 1);

        User user = (User) httpSession.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        List<Item> book_list = new ArrayList<Item>();
        if (keyword.equals("全部旧书"))
            book_list = bookService.getallBook(user.getUserid(), starts, limit, sortStr);
        else
            book_list = bookService.searchBook(keyword, user.getUserid(), starts, limit, sortStr);
        System.out.println("一共收到：" + book_list.size());
        modelAndView.addModel("booklist", book_list);
        modelAndView.getModel().put("keyword", keyword);
        modelAndView.getModel().put("nextsort", nextsort);
        modelAndView.getModel().put("sort", sort);
        modelAndView.getModel().put("start", start);
        modelAndView.setPath("search.jsp");
        return modelAndView;

    }


    @RequestMapping("/search")
    public ModelAndView search(HttpServletRequest request) {
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        if(user==null){
            modelAndView.setPath("index.html");
            return modelAndView;
        }
        List<Item> book_list = bookService.getallBook(user.getUserid(), 0, 8, "itemid desc");
        System.out.println("【/book/search】一共搜索到：" + book_list.size() + "条图书记录。");
        modelAndView.addModel("booklist", book_list);
        modelAndView.getModel().put("keyword", "全部旧书");
        modelAndView.setPath("search.jsp");
        return modelAndView;

    }

    @RequestMapping(value = "/addBook.do",method = "post")
    public ModelAndView addBook(@RequestParam("bookname") String bookName,
                                @RequestParam("price") String price,
                                @RequestParam("press") String press,
                                @RequestParam("version") String version,
                                @RequestParam("author") String author,
                                @RequestParam("kind") String kind,
                                @RequestParam("other") String other,
                                @RequestParam("imageurl") String imageUrl,
                                HttpServletRequest request) {
        System.out.println("getBookname========" + bookName);
        Item item = new Item();
        item.setAuthor(author);
        item.setBookname(bookName);
        //item.setImages(imageUrl);
        item.setKind(kind);
        item.setOther(other);
        item.setPress(press);
        price=price.replace("元", "");
        price=price.replace("￥", "");
        item.setPrice(Float.valueOf(price).intValue());
        item.setVersion(version);
        HttpSession httpSession = request.getSession();

        User user = (User) httpSession.getAttribute("user");
        item.setSellerid(user.getUserid());
        System.out.println("sellerid=======" + user.getUserid());
        bookService.addBook(item, user);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setPath("sellinfo.jsp");
        return modelAndView;
    }


}
